package multithread.framework.tool;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import multithread.framework.utility.Utilities;
import javax.swing.JCheckBox;
/**
 * 
 * @author dailey_dai
 *
 */
public class GenerateUI extends JFrame implements FilenameFilter {

	private static final long serialVersionUID = -2899480096039537266L;
	private JPanel contentPane;
	private JTextField textField;
	private JButton btnsearch;
	private JButton btnGenerate;
	// success generate file number
	private int count = 0;
	private JFileChooser fileInputChooser;
	private JFileChooser fileOutputChooser;
	private File lastInputFile;
	private File lastOutputFile;
	private JTextField textFieldOutput;
	private JCheckBox chkOutput;
	private JButton btnOutputSearch;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GenerateUI frame = new GenerateUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GenerateUI() {
		setTitle("TestCaseXMLGenerator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 438, 140);
		contentPane = new JPanel();
		contentPane
				.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("TestCase");
		label.setBounds(10, 33, 58, 14);
		contentPane.add(label);

		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(66, 29, 236, 23);
		contentPane.add(textField);

		fileInputChooser = new JFileChooser();
		fileInputChooser.setDialogTitle("Choose Test Case Class File&Director");
		fileInputChooser
				.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		btnsearch = new JButton("...");
		btnsearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (lastInputFile != null) {
					fileInputChooser.setCurrentDirectory(lastInputFile);
				}
				int returnVal = fileInputChooser
						.showOpenDialog(GenerateUI.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File selectedFile = fileInputChooser.getSelectedFile();
					if (selectedFile.isFile()) {
						lastInputFile = selectedFile.getParentFile();
					} else {
						lastInputFile = selectedFile;
					}
					GenerateUI.this.textField.setText(selectedFile
							.getAbsolutePath());
				}
			}
		});

		btnsearch.setBounds(299, 29, 30, 23);
		contentPane.add(btnsearch);

		btnGenerate = new JButton("Generate");
		btnGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count = 0;
				boolean throwException = false;
				try {
					generateTestCase(new File(GenerateUI.this.textField
							.getText()));
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(GenerateUI.this,
							"Error message:" + e1.getMessage(), "Error",
							JOptionPane.ERROR_MESSAGE);
					throwException = true;
				}
				if (!throwException)
					JOptionPane.showMessageDialog(GenerateUI.this, count
							+ " test case generate.");
			}
		});
		btnGenerate.setBounds(331, 29, 89, 23);
		contentPane.add(btnGenerate);

		JLabel lblOutput = new JLabel("Output");
		lblOutput.setBounds(10, 69, 46, 14);
		contentPane.add(lblOutput);

		textFieldOutput = new JTextField();
		textFieldOutput.setEditable(false);
		textFieldOutput.setEnabled(false);
		textFieldOutput.setBounds(66, 65, 235, 23);
		contentPane.add(textFieldOutput);
		textFieldOutput.setColumns(10);

		fileOutputChooser = new JFileChooser();
		fileOutputChooser.setDialogTitle("Choose Save Director");
		fileOutputChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		btnOutputSearch = new JButton("...");
		btnOutputSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (lastOutputFile != null) {
					fileOutputChooser.setCurrentDirectory(lastOutputFile);
				}
				int returnVal = fileOutputChooser
						.showOpenDialog(GenerateUI.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File selectedFile = fileOutputChooser.getSelectedFile();
					GenerateUI.this.textFieldOutput.setText(selectedFile
							.getAbsolutePath());
					lastOutputFile = selectedFile;
				}

			}
		});
		btnOutputSearch.setEnabled(false);
		btnOutputSearch.setBounds(299, 65, 30, 23);
		contentPane.add(btnOutputSearch);

		chkOutput = new JCheckBox("");
		chkOutput.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean enable = false;
				if (GenerateUI.this.chkOutput.isSelected()) {
					enable = true;
				} else {
					textFieldOutput.setText(null);
					lastOutputFile = null;
				}
				textFieldOutput.setEnabled(enable);
				btnOutputSearch.setEnabled(enable);

			}
		});
		chkOutput.setBounds(331, 65, 82, 23);
		contentPane.add(chkOutput);
	}

	/**
	 * generate test case XML file
	 * @param selectedFile File
	 */
	protected void generateTestCase(File selectedFile) {
		if (selectedFile.isFile()) {
			String fileName = selectedFile.getName().substring(0,
					selectedFile.getName().lastIndexOf("."));
			File xmlfile = new File(selectedFile.getParentFile(), fileName
					+ ".xml");
			if (lastOutputFile != null && chkOutput.isSelected()) {
				xmlfile = new File(lastOutputFile, fileName + ".xml");
			}
			TestCaseGenerator generator = new TestCaseGenerator(
					new XMLGenerator(xmlfile));
			generator.write(Utilities.newInstance(Utilities
					.findClass(selectedFile)));
			if (generator.generate() != null) {
				count++;
			}
		} else if (selectedFile.isDirectory()) {
			for (File file : selectedFile.listFiles(this)) {
				generateTestCase(file);
			}
		}
	}

	@Override
	public boolean accept(File dir, String name) {
		if (name.contains("$") || !name.endsWith(".class"))
			return false;
		else
			return true;
	}
}
